package com.woniu.lottery.strategy;

import com.woniu.lottery.entity.LotteryContext;
import com.woniu.lottery.entity.LotteryPrize;
import com.woniu.lottery.entity.LotteryResult;
import org.springframework.stereotype.Component;

import java.util.List;

// 固定概率抽奖
@Component
public class ProbabilityLotteryAlgorithm implements LotteryAlgorithm {

    @Override
    public LotteryResult draw(LotteryContext context) {

        //获取可用的奖品列表
        List<LotteryPrize> prizes = context.getPrizes();
        double random = Math.random(); // 生成0-1之间的随机数

        double tempProbability = 0.0;
        for (LotteryPrize prize : prizes) {
            // 累加概率区间
            tempProbability += prize.getProbability().doubleValue();

            if (random <= tempProbability) {
                if (prize.getRemainingAmount() > 0) {
                    return LotteryResult.success(
                            prize.getId(),
                            prize.getName(),
                            prize.getType(),
                            prize.getValue()
                    );
                }
            }
        }

        // 未中奖
        return LotteryResult.fail("很遗憾，未中奖");
    }
}
